package com.suke.czx.modules.reward.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.suke.czx.common.utils.FieldConstant;
import com.suke.czx.common.utils.R;
import com.suke.czx.modules.reward.entity.Reward;
import com.suke.czx.modules.reward.entity.RewardSearchBo;
import com.suke.czx.modules.reward.mapper.RewardMapper;
import com.suke.czx.modules.reward.service.RewardService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 应用模块名称: 奖惩
 * 代码描述: 奖惩
 * copyright: AUZAR ALL RIGHTS RESERVED
 * company: AUZAR
 *
 * @author: 颜米晨
 * @date: 2021/09/03 0:52
 */
@Service
@AllArgsConstructor
public class RewardServiceImpl extends ServiceImpl<RewardMapper, Reward> implements RewardService {

    private final RewardMapper rewardMapper;

    /**
     * 分页查询奖惩列表
     * @param rewardSearchBo 查询BO
     * @return VO
     */
    @Override
    public R pageRewardList(RewardSearchBo rewardSearchBo) {
        Page<Reward> page = PageHelper.startPage(rewardSearchBo.getPage(), rewardSearchBo.getLimit(), true,true,true);
        //获取资讯文章查询结果集
        List<Reward> infoList = rewardMapper.pageRewardList(rewardSearchBo);
        return Objects.requireNonNull(R.ok().put(FieldConstant.TOTAL, page.getTotal())).setData(Optional.ofNullable(infoList).orElse(Lists.newArrayList()));
    }

    /**
     * 删除奖惩
     * @param rewardInfo 奖惩编码
     * @param userId 操作者编码
     * @return VO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateDelReward(Long rewardInfo, Long userId) {
        rewardMapper.updateDelReward(rewardInfo, userId);
        return R.ok();
    }

}
